<template>
  <div>
    <div v-if="this.$route.name == 'medicinalEdition'" class="app-container">
      <vue-element-loading
        :active="this.$store.state.elementloading.vueElementLoading"
        spinner="spinner"
        color="#FF6700"
      />
      <el-card shadow="never">
        <el-form
          ref="searchForm"
          :inline="true"
          :model="searchMap"
          style="margin-top: 20px;margin-left: 0px"
        >
          <el-form-item prop="mName">
            <el-input @keyup.enter.native="searchMedicinal" v-model.lazy="searchMap.mName">
              <template slot="prepend">药材名称</template>
            </el-input>
          </el-form-item>
          <el-button @click="searchMedicinal" icon="el-icon-search"></el-button>
        </el-form>

        <template>
          <el-table
            :data="list"
            max-height="500"
            border
            stripe
            style="width: 100%;margin-top: 10px"
            @row-click="chooseone"
            @row-dblclick="openDetails"
            highlight-current-row
            @current-change="handleSelectionChange"
          >
            <el-table-column fixed width="55">
              <template slot-scope="scope">
                <el-radio v-model="radio" :label="scope.row.id">
                  <span class="el-radio__label"></span>
                </el-radio>
              </template>
            </el-table-column>
            <el-table-column prop="eTime" label="版本时间" width="80"></el-table-column>
            <el-table-column prop="mName" label="药材名称" width="80"></el-table-column>
            <el-table-column prop="mLatinName" label="药材拉丁名" width="100"></el-table-column>
            <el-table-column prop="mOriginName" label="基源名称" width="80"></el-table-column>
            <el-table-column align="center" prop="mOriginLatinName" label="基源拉丁名" width="95"></el-table-column>
            <el-table-column prop="mMethod" label="药用部位" width="100"></el-table-column>
            <el-table-column prop="mOriginInterview" label="基源简介"></el-table-column>
            <el-table-column prop="mRemarks" label="药材简介"></el-table-column>
            <el-table-column align="center" prop="mPriceLink" label="价格查看" width="95">
              <template slot-scope="scope">
                <el-button @click="showprice(scope.row.mPriceLink)" type="primary" size="mini">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="mCreateTime" label="创建时间" width="100"></el-table-column>
          </el-table>
        </template>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-card>
    </div>
    <router-view />
  </div>
</template>

<script>
import VueElementLoading from "vue-element-loading";
import medicinalEditionApi from "@/api/medicinalEdition";
export default {
  components: {
    VueElementLoading
  },

  data() {
    return {
      total: 0, //总记录数
      currentPage: 1, //当前页码
      pageSize: 5, // 每页显示5条数据
      sum: 0,
      radio: null, // 如果使用单选框，定义一个model值
      list: [],
      pojo: {
        mName: "",
        page: "",
        size: "",
        eTime: ""
      },
      searchMap: {
        mName: ""
      }
    };
  },

  created() {
    this.fetchData();
  },

  watch: {
    $route(to, from) {
      this.fetchData();
    }
  },

  methods: {
    handleSizeChange(val) {
      console.log(`每页${val}条`);
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      console.log(`当前页${val}`);
      this.currentPage = val;
      this.fetchData();
    },

    handleSelectionChange(row) {
      console.log(row);
      this.currentSelectItem = row;
    },
    chooseone(row) {
      // this.checked = true;
      this.radio = row.id;
      if (this.sum == 0) {
        this.$notify({
          title: "温馨提示",
          message: "双击数据行可以查看详情信息哦！",
          type: "success",
          duration: 1000
        });
      }
      this.sum += 1;
    },

    fetchData() {
      this.pojo.mName = this.searchMap.mName;
      this.pojo.page = this.currentPage;
      this.pojo.size = this.pageSize;
      this.pojo.eTime = localStorage.getItem("eTime");
      medicinalEditionApi.select(this.pojo).then(resp => {
        this.list = resp.data.rows;
        this.total = resp.data.total;
      });
    },
    searchMedicinal() {
      this.currentPage = 1;
      this.fetchData();
    },
    // 双击一行
    openDetails(row) {
      console.log(row.id);
      const mid = row.id;
      this.$store.dispatch("addEditionMid", mid);
      this.$router.push({
        name: "medicinalproductbaseEdition"
      });
    },
    // 跳转价格页面
    showprice(link) {
      window.open(link, "_blank");
    }
  }
};
</script>